package com.example.ccj.b.c.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.example.ccj.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 加载选项view
 * @param <T>
 */
public class OptionChoiceControl <T> extends LinearLayout {
    //选项容器
    LinearLayout container;
    //选择的选项列表
    IMRadioButtonListener<T> selectListener;
    List<T> list;
    //加载选项的模式 单选or多选
    @MRadioButton.OptionChoiceMode int mode = MRadioButton.SINGLE;

    //当前加载的选项
    private List<MRadioButton<T>> mRadioButtonList;

    //主要控制单选项是否在选择后就判断正确与否
    boolean isCheck = false;

    String[] bianHao = new String[]{"A","B","C","D","E","F","G","H","I","J",};

    boolean isOnlyCheckOne = true;

    /**
     *
     * @param context 上下文
     * @param list 选项数据
     * @param mode 选项模式
     * @param isCheck 选择完毕是否检查
     * @param selectListener 回调
     */
    public OptionChoiceControl(Context context, List<T> list, @MRadioButton.OptionChoiceMode int mode, boolean isCheck,  IMRadioButtonListener<T> selectListener) {
        super(context);
        this.list = list;
        this.selectListener = selectListener;
        this.mode = mode;
        this.isCheck = isCheck;
        build();
    }

    public OptionChoiceControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LinearLayout.inflate(context, R.layout.custom_option_choice_control,this);
        container = findViewById(R.id.custom_option_choice_control_container);
    }
    List<T> selectedData = new ArrayList<>();

    /**
     * 判断选择的是否正确
     * @param isChange 是否改变已选择的显示状态
     * @return 是否正确
     */
    public boolean check(boolean isChange){
        boolean isCorrect = true;
        for (MRadioButton<T> radioButton : mRadioButtonList) {
            if (isOnlyCheckOne) radioButton.setReadOnly();
            //判断结果
            if (radioButton.isSelected && !radioButton.isRight) isCorrect = false;
            //改变显示状态
            if (isChange && radioButton.isSelected || radioButton.isRight) {
                radioButton.isCheck = true;
                radioButton.setSelected(true);
                selectListener.onCheckedChanged(radioButton, true);
            }
        }
        if (selectListener != null) selectListener.onJudge(selectedData,isCorrect);
        return isCorrect;
    }

    public void build(){
        mRadioButtonList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MRadioButton<T> radioButton = new MRadioButton<>(getContext());
            radioButton.setId(i);
            radioButton.setRadioButtonListener(list.get(i), new IMRadioButtonListener<T>() {
                @Override
                public CharSequence getOptionText(T t) {
                    return selectListener.getOptionText(t);
                }

                @Override
                public CharSequence getTag(T t) {
                    return selectListener.getTag(t);
                }

                @Override
                public boolean checkIsCorrect(T t) {
                    return selectListener.checkIsCorrect(t);
                }

                @Override
                public boolean isSelected(T t) {
                    return selectListener.isSelected(t);
                }

                @Override
                public boolean isCheck(T t) {
                    return selectListener.isCheck(t);
                }

                @Override
                public void onCheckedChanged(MRadioButton<T> radioButton, boolean isChecked) {
                    if (isChecked) {
                        if (mode == MRadioButton.SINGLE) {
                            removeOtherSelectedState(radioButton);
                            selectedData.clear();
                            selectedData.add(radioButton.getValue());
                            //自动判断对错
                            if (isCheck) check(true);
                        } else {
                            if (!selectedData.contains(radioButton.getValue())) {
                                selectedData.add(radioButton.getValue());
                            }
                        }
                    } else {
                        if (selectedData != null) selectedData.remove(radioButton.getValue());
                    }
                    selectListener.onCheckedChanged(radioButton, isChecked);
                }
            });
            radioButton.setMode(mode);
            radioButton.setTag(bianHao[i]);
            radioButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            radioButton.build();
            container.addView(radioButton);
            mRadioButtonList.add(radioButton);
        }
    }

    /**
     * 单选时去除其他选项
     * @param rb 选择的
     */
    private void removeOtherSelectedState(MRadioButton<T> rb){
        for (MRadioButton<T> radioButton : mRadioButtonList) {
            if (!radioButton.equals(rb)) radioButton.setSelected(false);
        }
    }

    /**
     * 获取已选择的选项
     * @return 已选择数据
     */
    public List<T> getSelectedData(){
        return selectedData;
    }

    public OptionChoiceControl<T> setSelectListener(IMRadioButtonListener<T> selectListener) {
        this.selectListener = selectListener;
        return this;
    }

    public OptionChoiceControl<T> setList(List<T> list) {
        this.list = list;
        return this;
    }

    public OptionChoiceControl<T> setMode(int mode) {
        this.mode = mode;
        return this;
    }

    public OptionChoiceControl<T> setCheck(boolean check) {
        isCheck = check;
        return this;
    }
}
